package com.tiancheng.trade.authserver.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiancheng.trade.authserver.dto.ApplicationAddUpdateDTO;
import com.tiancheng.trade.authserver.dto.ApplicationListDTO;
import com.tiancheng.trade.authserver.entity.AuthApplication;
import com.tiancheng.trade.authserver.model.query.ApplicationBaseInfoUpdateParam;
import com.tiancheng.trade.authserver.model.query.ApplicationQuery;
import com.tiancheng.trade.authserver.model.reponse.ApplicationBaseInfo;
import com.tiancheng.trade.authserver.service.IAuthApplicationService;
import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.exception.AuthorizationException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping({"/application", "/thirdApplication"})
public class ApplicationController {
    @Resource
    private IAuthApplicationService service;
    @Resource
    private HttpServletRequest request;

    @GetMapping("/getPageInfo")
    //@RequiresPermissions({"auth:application:query"})
    public Result<PageInfo<ApplicationListDTO>> getPageInfo(ApplicationQuery param) {
        param.setDataPermissionCode(null);
        return Result.success(this.service.queryPage(param));
    }

    @PostMapping("/save")
    //@RequiresPermissions({"auth:application:add"})
    public Result<Long> save(@RequestBody ApplicationAddUpdateDTO param) {
        return Result.success(this.service.saveByDTO(param));
    }

    /**
     * 更新应用基础信息
     */
    @PutMapping("/admin/updateBaseInfo")
    //@RequiresPermissions({"auth:application:updateBaseInfo"})
    public Result<Void> admin_updateBaseInfo(@RequestBody ApplicationBaseInfoUpdateParam param) {
        this.service.updateBaseInfo(param);
        return Result.success();
    }

    /**
     * 获取应用基础信息
     */
    @GetMapping("/admin/getBaseInfo")
    //@RequiresPermissions({"auth:application:getBaseInfo"})
    public Result<ApplicationBaseInfo> admin_getBaseInfo(@RequestParam String id) {
        return Result.success(this.service.getBaseInfo(id));
    }

    @DeleteMapping("/delete")
    //@RequiresPermissions({"auth:application:delete"})
    public Result<Void> delete(@RequestParam String id) {
        this.service.delete(id);
        return Result.success();
    }

    @GetMapping("/getBaseInfo")
    public Result<Map<String, Object>> getBaseInfo(@RequestParam String clientId) {
        AuthApplication application = this.service.getOne(new LambdaQueryWrapper<AuthApplication>().eq(AuthApplication::getClientId,clientId));
        if (application == null) {
            throw new AuthorizationException(401, "无效三方应用");
        }
        Map<String, Object> data = new HashMap<>();
        data.put("name", application.getName());
        data.put("iconUrl", application.getIconUrl());
        return Result.success(data);
    }

}
